package com.group6.exam.manager.dao;

import java.util.List;
import java.util.Map;

import com.group6.exam.common.entity.PageModel;
import com.group6.exam.common.util.CollectionUtil;
import com.group6.exam.common.util.DBUtil;

public class RoleDaoImpl implements IRoleDao {

	@Override
	public List<Map<String, Object>> getAllRole() {
		String sql = "select * from roleInfo";

		return DBUtil.executeQuery(sql);
	}

	@Override
	public List<Map<String, Object>> check(PageModel pageModel) {

		String roleId = pageModel.getRoleId();
		int index = (pageModel.getCurrentPage() - 1) * pageModel.getPageSize();
		int pageSize = pageModel.getPageSize();
		String sql = "select * from userInfo where role_id=? LIMIT ?, ?";

		return DBUtil.executeQuery(sql, roleId, index, pageSize);
	}

	/**
	 * 更具roleId 加载角色名
	 */
	public String getRoleName(String role_id) {
		String sql = "select role_name from roleInfo where role_id = ?";
		List<Map<String, Object>> list = DBUtil.executeQuery(sql, role_id);

		return list != null && list.size() > 0 ? list.get(0).get("role_name")
				+ "" : null;
	}

	/**
	 * 角色删除
	 */
	public int deleteRole(String roleId) {
		String sql = "delete from roleInfo where role_id=?";

		return DBUtil.executeUpdate(sql, roleId);
	}

	/**
	 * 角色新增
	 */
	public int addRole(String roleName) {
		String sql = "insert into roleInfo values(null, ?)";

		return DBUtil.executeUpdate(sql, roleName);
	}

	@Override
	public Map<String, Object> initRole(String roleId) {
		String sql = "select * from roleInfo where role_id=?";
		List<Map<String, Object>> list = DBUtil.executeQuery(sql, roleId);

		return list != null && list.size() > 0 ? list.get(0) : null;
	}

	/**
	 * 修改角色
	 */
	public int update(String roleName, String roleId) {
		String sql = "update roleInfo set role_name=? where role_id=?";

		return DBUtil.executeUpdate(sql, roleName, roleId);
	}

	/**
	 * 查询总条数
	 */
	public int getTotalRecords(String roleId) {
		String sql = "select * from userInfo where role_id=?";

		return DBUtil.executeQuery(sql, roleId).size();
	}

	/**
	 * 查询系统中所有权限json字符串
	 */
	@Override
	public List<Map<String, Object>> getRights() {
		String sql = "select * from rightinfo";

		return DBUtil.executeQuery(sql);
	}

	@Override
	public List<Map<String, Object>> getMeunsByRoleId(String roleId) {
		// 定义SQL命令
		String sql = "select * from rightinfo where right_id in (select right_id from rolerightinfo where role_id=?)";
		return DBUtil.executeQuery(sql, roleId);
	}

	@Override
	public boolean isGrant(String roleId) {
		String sql = "select 1 from rolerightinfo where role_id=?";
		List<Map<String, Object>> list = DBUtil.executeQuery(sql, roleId);
		return CollectionUtil.isEmpty(list);
	}

	@Override
	public int permission(String rightId, String roleId) {
		String sql = "insert into rolerightinfo values(null,?,?)";

		return DBUtil.executeUpdate(sql, rightId, roleId);
	}

	@Override
	public int revoke(String roleId) {
		String sql = "delete from rolerightinfo where role_id=?";

		return DBUtil.executeUpdate(sql, roleId);
	}

}
